<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .banner {
            width: 500px;
            margin: 0 auto;
            text-align: center;
        }

        .banner img {
            width: 100%;
            height: 300px;
            object-fit: contain;
            border: 1px solid;
            display: block;
        }
    </style>
</head>

<body>
    <div class="banner">
        <img id="img" src="img/1.jpg" alt="">
        <p>
            <button id="btnPrev">上一张</button>
            <button id="btnNext">下一张</button>
        </p>
    </div>

    <script>
        var i = 1;
        var img = document.getElementById("img"),
            btnPrev = document.getElementById("btnPrev"),
            btnNext = document.getElementById("btnNext");

        btnPrev.onclick = function() {
            i--;
            if (i === 0) {
                i = 5;
            }
            img.src = `img/${i}.jpg`;
        }

        btnNext.onclick = function() {
            i++;
            if (i === 6) {
                i = 1;
            }
            img.src = `img/${i}.jpg`;
        }
    </script>
</body>

</html>
